package ru.infosystem.client.frame;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

/**
 *
 * @author akoshi
 */

public class OptionBox extends javax.swing.JDialog {

    private Properties properties = new Properties();  
    
    private String     properiesFile;
    
    public OptionBox(java.awt.Frame parent, boolean modal, 
            String properiesFile) {
        
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        
        this.properiesFile = properiesFile;
        
        try {
            
            File pfile = new File(properiesFile);

            if(!pfile.exists())
                pfile.createNewFile();

            FileInputStream fileInputStream = new FileInputStream(pfile);
            properties.load(fileInputStream);
            
            ipField.setText(properties.getProperty("InetAddress"));
            portField.setText(properties.getProperty("Port"));
            
        } catch (IOException ex) {
            
             System.err.println("Couldn't init client: " + ex.getMessage());
        }
    }
    
    private void editPropertiesFile() {
        
         try {
             
            File pFile = new File(properiesFile);
            
            if (!pFile.exists()) 
                pFile.createNewFile();
            
            FileWriter fileWriter = new FileWriter(pFile);
            
            properties.setProperty("InetAddress", ipField.getText());
            properties.setProperty("Port", portField.getText());

            properties.store(fileWriter, null);
            
        } catch(IOException ex) {
            
            System.err.println("Couldn't init client: " + ex.getMessage());
            
        } finally {
            dispose();
        }       
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        portLabl = new javax.swing.JLabel();
        portField = new javax.swing.JTextField();
        ipField = new javax.swing.JTextField();
        okButn = new javax.swing.JButton();
        ipLabl = new javax.swing.JLabel();
        optionLabl = new javax.swing.JLabel();
        canclButn = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        portLabl.setText("Server port:");

        portField.setToolTipText("");

        ipField.setToolTipText("");

        okButn.setText("Ok");
        okButn.setToolTipText("");
        okButn.setPreferredSize(new java.awt.Dimension(81, 25));
        okButn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButnActionPerformed(evt);
            }
        });

        ipLabl.setText("Server ip:");

        optionLabl.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
        optionLabl.setText("Connection options:");
        optionLabl.setToolTipText("");

        canclButn.setText("Cancel");
        canclButn.setToolTipText("");
        canclButn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                canclButnActionPerformed(evt);
            }
        });

        jSeparator1.setBackground(new java.awt.Color(0, 0, 0));
        jSeparator1.setForeground(new java.awt.Color(0, 0, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jSeparator1))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(optionLabl))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(19, 19, 19)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(ipLabl)
                                .addComponent(portLabl))
                            .addGap(35, 35, 35)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(portField, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(ipField, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(okButn, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(canclButn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(optionLabl)
                .addGap(7, 7, 7)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ipField, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ipLabl))
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(portField, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(portLabl))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(canclButn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void canclButnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_canclButnActionPerformed
        dispose();
    }//GEN-LAST:event_canclButnActionPerformed

    private void okButnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButnActionPerformed
       editPropertiesFile();       
    }//GEN-LAST:event_okButnActionPerformed
    /*
     * 
     * 
     */    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton canclButn;
    private javax.swing.JTextField ipField;
    private javax.swing.JLabel ipLabl;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton okButn;
    private javax.swing.JLabel optionLabl;
    private javax.swing.JTextField portField;
    private javax.swing.JLabel portLabl;
    // End of variables declaration//GEN-END:variables
}
